package com.behome.netty.netty_D.bean;


import com.behome.netty.common.ByteConventerKit;

import java.nio.ByteBuffer;

public class PacketHead {
    public static final byte[] FLAG = new byte[]{-2, -3};
    public static final short LENGTH = 12;
    public static final int INDEX_PACKET_LENGTH = 10;
    static final byte[] VERSION = new byte[]{1, 0};
    int sessionId;
    PacketType type;
    private byte signFlag;
    short packetLength;
    static final int HEAD_LENGTH = 12;

    public PacketHead() {
    }

    public PacketHead(int sessionId, PacketType packetType, boolean isSign, short packetLength) {
        this.sessionId = sessionId;
        this.type = packetType;
        this.signFlag = (byte)(isSign?1:0);
        this.packetLength = packetLength;
    }

    public int getSessionId() {
        return this.sessionId;
    }

    public void setSessionId(int sessionId) {
        this.sessionId = sessionId;
    }

    public PacketType getType() {
        return this.type;
    }

    public void setType(PacketType type) {
        this.type = type;
    }

    public boolean isSign() {
        return this.signFlag != 0;
    }

    public void setIsSign(boolean isSign) {
        this.signFlag = (byte)(isSign?1:0);
    }

    public byte getSignFlag() {
        return this.signFlag;
    }

    public void setSignFlag(byte signFlag) {
        this.signFlag = signFlag;
    }

    public short getPacketLength() {
        return this.packetLength;
    }

    public void setPacketLength(short packetLength) {
        this.packetLength = packetLength;
    }

    public byte[] serialize() {
        if(this.type == PacketType.UNKNOWN) {
            throw new IllegalArgumentException("数据包类型type为非法值,不能序列化消息头.");
        } else if(this.packetLength <= 0) {
            throw new IllegalArgumentException("数据包长度packetLength不能为空,不能序列化消息头.");
        } else {
            ByteBuffer byteBuffer = ByteBuffer.allocate(12);
            byteBuffer.put(FLAG).put(VERSION).put(ByteConventerKit.intToBytes2(this.sessionId)).put(this.type.value).put(this.signFlag).put(ByteConventerKit.shortToBytes(this.packetLength));
            return byteBuffer.array();
        }
    }

    public static PacketHead deserialize(byte[] headData) {
        PacketHead ret = new PacketHead();
        if(headData != null && headData.length == 12) {
            ByteBuffer byteBuffer = ByteBuffer.wrap(headData);
            byte[] flagData = new byte[2];
            byteBuffer.get(flagData);
            if(flagData[0] == FLAG[0] && flagData[1] == FLAG[1]) {
                byte[] versionData = new byte[2];
                byteBuffer.get(versionData);
                ret.setSessionId(byteBuffer.getInt());
                ret.setType(PacketType.getPacketTypeByValue(byteBuffer.get()));
                ret.setSignFlag(byteBuffer.get());
                ret.setPacketLength(byteBuffer.getShort());
            }
        }

        return ret;
    }

    public static enum PacketType {
        UNKNOWN((byte)0),
        REQUEST((byte)1),
        RESPONSE((byte)2);

        byte value;

        private PacketType(byte value) {
            this.value = value;
        }

        public static PacketType getPacketTypeByValue(byte value) {
            PacketType[] var1 = values();
            int var2 = var1.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                PacketType item = var1[var3];
                if(item.value == value) {
                    return item;
                }
            }

            return UNKNOWN;
        }
    }
}
